package com.party.controller.app;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.ObjectMetadata;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.party.common.base.ApiResult;
import com.party.common.entity.MyPage;
import com.party.entity.*;
import com.party.entity.dto.PublishBannerDto;
import com.party.entity.dto.QueryUserDto;
import com.party.entity.dto.ReqLoginDto;
import com.party.entity.vo.SelectDeviceVo;
import com.party.service.PbUserService;
import com.party.util.JwtTokenUtil;
import com.party.util.WxUntill;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController("AppPbUserController")
@RequestMapping("app/pbUser")
@Api(tags = "小程序用户相关api接口", value = "小程序用户相关api接口")
public class PbUserController {

    @Autowired
    private PbUserService pbUserService;

    @PostMapping("login")
    @ApiOperation("登录系统")
    public ApiResult login(@RequestBody ReqLoginDto loginReqDao){
        return pbUserService.login(loginReqDao);
    }

    @ApiOperation("通过openId获取用户信息")
    @PostMapping(value = "/getUserInfo")
    public ApiResult<PbUser> getUserInfo(@RequestBody ReqLoginDto param) throws IOException {
        //先根据传的code 获取openid
        String wxOpenid = WxUntill.getWxOpenid(param.getCode());
        //查找openid
        LambdaQueryWrapper<PbUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PbUser::getOpenId, wxOpenid);
        PbUser one = pbUserService.getOne(queryWrapper);
        return ApiResult.ok(one);
    }

    @ApiOperation("获取用户openId")
    @GetMapping("/getOpenId/{code}")
    public ApiResult<String> getUserInfo(@PathVariable("code") String code) throws IOException {
            String wxOpenid = WxUntill.getWxOpenid(code);
            return ApiResult.ok(wxOpenid);

    }

    @ApiOperation("通过用户openId获取用户阅读记录")
    @PostMapping("/getResourceRecord")
    public ApiResult<MyPage<PbUserResourceHistory>> getResourceRecord(@RequestBody QueryUserDto dto) {
        return ApiResult.ok(pbUserService.getResourceRecord(dto));

    }
    @ApiOperation("通过用户id获取用户设备列表")
    @PostMapping("/getDeviceList")
    public ApiResult<MyPage<PbDevice>> getDeviceList(@RequestBody QueryUserDto dto) {
        return ApiResult.ok(pbUserService.getDeviceList(dto));

    }

    @PostMapping("publish")
    @ApiOperation(value = "发布 类型1-自控电台 2-党建风采 3-轮播海报 4-主题日历",response = Boolean.class)
    public ApiResult<Boolean> publish(@RequestBody PublishBannerDto dto) {
        return ApiResult.ok(pbUserService.publish(dto));
    }

    @GetMapping("selectDevice")
    @ApiOperation(value = "设备下拉框",response = Boolean.class)
    public ApiResult<List<SelectDeviceVo>> selectDevice(Long id) {
        return ApiResult.ok(pbUserService.selectDevice(id));
    }
}
